package cn.tedu.controller;

import cn.tedu.controller.BaseAndEx.BaseController;
import cn.tedu.entity.Address;
import cn.tedu.service.port.AddressService;
import cn.tedu.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/address")
public class AddressControler extends BaseController {

    @Resource
    private AddressService addressService;

    /**
     * 增加收货地址
     * @param session   用于获取uid
     * @param address   地址对象
     * @return 返回json对象
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/add.do")
    public JsonResult handleADdd(HttpSession session, Address address){
        Integer uid = getUidSession(session);
        address.setUid(uid);
        addressService.add(address);
        return new JsonResult();
    }

    /**
     * 通过uid查询该用户已添加的地址信息
     * @param session   用于获取uid
     * @return  成功返回查询到的Address地址集合
     */
    @ResponseBody
    @RequestMapping("/get_list.do")
    public JsonResult getAddressListByUid(HttpSession session){
        Integer uid = getUidSession(session);
        List<Address> list = addressService.selectAddress(uid);
        return new JsonResult(list);
    }

    /**
     * 通过id删除已选中要删除的地址信息
     * @param id    已选中信息id
     */
    @ResponseBody
    @RequestMapping(value = "/delete_address.do", method = RequestMethod.POST)
    public JsonResult deleteAddressById(Integer id, HttpSession session){
        Integer uid = getUidSession(session);
        addressService.deleteAddress(id, uid);
        return new JsonResult();
    }

    /**
     * 通过id修改已选中的地址信息
     * @param address   地址信息对象
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/handle_update.do")
    public JsonResult updateAddressById(Address address, Integer id, HttpSession session){
        Integer uid = getUidSession(session);
        addressService.updateAddress(address, id, uid);
        return new JsonResult();
    }

    /**
     * 根据id查询address地址对象
     * @param id    用户地址id
     * @param session   用于获取用户id
     * @return  返回address地址对象
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/handle_select.do")
    public JsonResult getAddressByIdAndUid(Integer id, HttpSession session){
        Integer uid = getUidSession(session);
        Address address = addressService.getAddressByIdAndUid(id, uid);
//        System.out.println(address.getRecvCity());
        return new JsonResult(address);
    }

    /**
     * 根据用户uid和该地址id将选中地址状态设置为默认，并将未选中地址取消默认状态
     * @param session   用于获取用户id
     * @param id    用户地址id
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "set_default.do")
    public JsonResult handleSetDefault(HttpSession session, Integer id){
        Integer uid = getUidSession(session);
        addressService.setDefault(uid, id);
        return new JsonResult();
    }


/*------------------------------------------------------------------------------------------------------------*/

    /**
     * 跳转到增加收货地址页面
     */
    @RequestMapping("/list.do")
    public String showList(){
        return "address_list";
    }
}
